
/**   
*    
* 项目名称：a_yongan   
* 类名称：GoodTypeServiceImpl   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月18日 下午2:10:43    
* 修改时间：2018年6月18日 下午2:10:43   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.yongan.user.enums.UserStatEnum;
import com.yongan.user.exception.UserException;
import com.yongan.util.Execution;
import com.yongan.warehouse.dao.GoodTypeDao;
import com.yongan.warehouse.entity.GoodType;
import com.yongan.warehouse.service.GoodTypeService;

/**
 * @author liubo
 *
 */
@Service
public class GoodTypeServiceImpl implements GoodTypeService{

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private GoodTypeDao goodTypeDao;
	private final String slat = "dsjdajsdkHJJHAJAKdsd921e0p12*DAe#@$@!qew";
	@Override
	public List<GoodType> getGoodTypeList(int offset, int limit) {
		return goodTypeDao.queryAll(offset, limit);
	}

	
	@Override
	public GoodType getGoodTypeById(String goodTypeId) {
		
		return goodTypeDao.queryById(goodTypeId);
	}

	
	@Override
	public Execution executeGoodType(GoodType goodType, String goodTypeId,
			String md5) throws UserException {
		if(md5 == null || !md5.equals(getMD5(goodTypeId))){
			//throw new UserException("user data rewrite");
		}
		try{
			if(goodType == null){
				int deleteCount = goodTypeDao.deleteGoodType(goodTypeId);
				if(deleteCount<=0){
					throw new UserException("delete failed");
				}else{
					return new Execution(goodTypeId, UserStatEnum.SUCCESS);
				}
			}else if((goodTypeId.equals(""))){
				int addCount = goodTypeDao.insertGoodType(goodType);
				if(addCount<=0){
					throw new UserException("add failed");
				}else{
					return new Execution(goodType.getGoodtypeId(), UserStatEnum.SUCCESS);
				}
			}else if(!(goodTypeId.equals(""))){
				int updateCount = goodTypeDao.updateGoodType(goodType);
				if(updateCount<=0){
					throw new UserException("update failed");
				}else{
					return new Execution(goodTypeId, UserStatEnum.SUCCESS);
				}
			}else{
				throw new UserException("parameter error");
			}
			
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			throw new UserException("inner error:"+e.getMessage());
		}
	}

	public int getCount() {
		
		return goodTypeDao.queryCount();
	}
	private String getMD5(String Id){
		String base = Id + "/" + slat;
		String md5 = DigestUtils.md5DigestAsHex(base.getBytes());
		return md5;
	}


	@Override
	public List<GoodType> getAllTree() {
		return goodTypeDao.queryAllTree();
	}

}
